package com.jeeplus.test.jlnu.changeStuInfo.controller;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jeeplus.test.jlnu.changeStuInfo.domain.entity.ChangeStuInfo;
import com.jeeplus.test.jlnu.changeStuInfo.domain.entity.ChangeStuInfoHistory;
import com.jeeplus.test.jlnu.changeStuInfo.domain.vo.ChangeStuInfoReq;
import com.jeeplus.test.jlnu.changeStuInfo.service.ChangeStuInfoDetailService;
import com.jeeplus.test.tools.resp.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Api(tags = "学籍信息")
@RestController
@Slf4j
@RequestMapping("/GetChangeStuInfo")
public class ChangeStuInfoDetailController {

    @Autowired
    ChangeStuInfoDetailService changeStuInfoDetailService;


    ////测试数据查询后续会删除
    @ApiOperation("学籍修改信息分页查询")
    @GetMapping("/page")
    public ResponseEntity<Page<Map<String, Object>>> getChangeStu(ChangeStuInfoReq changeStuInfo, @RequestParam(required = true) String officeId,
                                                                  @RequestParam(defaultValue = "1") int pageNum,
                                                                  @RequestParam(defaultValue = "10") int pageSize) {
        Page<ChangeStuInfo> page = new Page<>(pageNum, pageSize);
        Page<Map<String, Object>> pageResult = changeStuInfoDetailService.getChangeStu(changeStuInfo,officeId,page);

        List<Map<String, Object>> modifiedRecords = pageResult.getRecords().stream()
                .map(record -> {
                    Map<String, Object> modifiedRecord = new HashMap<>();
                    record.forEach((key, value) -> modifiedRecord.put(key, value != null ? value : ""));
                    return modifiedRecord;
                })
                .collect(Collectors.toList());

        Page<Map<String, Object>> modifiedPage = new Page<>(pageResult.getCurrent(), pageResult.getSize(), pageResult.getTotal());
        modifiedPage.setRecords(modifiedRecords);

        return ResponseEntity.ok()
                .contentType(MediaType.APPLICATION_JSON)
                .body(modifiedPage);
    }

    @ApiOperation("学籍修改信息历史记录查询")
    @GetMapping("/detailList")
    public ResponseEntity<List<ChangeStuInfoHistory>> getChangeStuInfoHistoryList(
            @RequestParam(required = true) Long id) {

        // 假设 changeStuInfoDetailService 有一个方法可以直接返回 List<ChangeStuInfoHistory>
        List<ChangeStuInfoHistory> changeStuInfoHistoryList = changeStuInfoDetailService.getChangeStu(id);

        // 如果需要处理 null 值，可以在这里进行处理
        List<ChangeStuInfoHistory> modifiedRecords = changeStuInfoHistoryList.stream()
                .map(record -> {
                    ChangeStuInfoHistory modifiedRecord = new ChangeStuInfoHistory();
                    // 假设 ChangeStuInfoHistory 有一个 copy 方法或者你可以逐个字段进行复制
                    // modifiedRecord.copy(record);
                    // 或者逐个字段进行复制
                    modifiedRecord.setId(record.getId());
                    modifiedRecord.setInfoId(record.getInfoId());
                    modifiedRecord.setField(record.getField());
                    modifiedRecord.setFieldName(record.getFieldName());
                    modifiedRecord.setDataHistory(record.getDataHistory() != null ? record.getDataHistory() : "");
                    modifiedRecord.setDataChange(record.getDataChange() != null ? record.getDataChange() : "");

                    modifiedRecord.setDataHistory(record.getDataHistory() != null ? record.getDataHistory() : "");
                    modifiedRecord.setDataChange(record.getDataChange() != null ? record.getDataChange() : "");
                    return modifiedRecord;
                })
                .collect(Collectors.toList());

        return ResponseEntity.ok()
                .contentType(MediaType.APPLICATION_JSON)
                .body(modifiedRecords);
    }

    //批量移交
    @ApiOperation("批量移交")
    @PostMapping("/batchUpdateChangeInfo")
    public R batchUpdateChangeInfo(@RequestBody ChangeStuInfo changeStuInfo) {
        // 参数校验
        if (CollectionUtils.isEmpty(changeStuInfo.getIds())) {
            return R.error("9999", "请选择需要移交的数据");
        }
        changeStuInfoDetailService.batchUpdateChangeInfo(changeStuInfo);
        return R.suc();
    }

}
